<template>
  <div class="layout-container">
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="200px">
        <div class="logo">
          <img src="@/assets/logo.svg" alt="Logo" class="logo-img" />
          <h3>Plus Admin</h3>
        </div>
        <el-menu
          :default-active="activeMenu"
          router
          background-color="#001428"
          text-color="#bfcbd9"
          active-text-color="#409EFF"
        >
          <el-menu-item index="/">
            <el-icon><HomeFilled /></el-icon>
            <span>首页</span>
          </el-menu-item>
          
          <el-sub-menu index="/system">
            <template #title>
              <el-icon><Setting /></el-icon>
              <span>系统管理</span>
            </template>
            <el-menu-item index="/users">
              <el-icon><User /></el-icon>
              <span>用户管理</span>
            </el-menu-item>
            <el-menu-item index="/roles">
              <el-icon><Lock /></el-icon>
              <span>角色管理</span>
            </el-menu-item>
            <el-menu-item index="/departments">
              <el-icon><OfficeBuilding /></el-icon>
              <span>部门管理</span>
            </el-menu-item>
            <el-menu-item index="/menus">
              <el-icon><Menu /></el-icon>
              <span>菜单管理</span>
            </el-menu-item>
          </el-sub-menu>

          <el-sub-menu index="/analytics">
            <template #title>
              <el-icon><DataAnalysis /></el-icon>
              <span>分析页</span>
            </template>
            <el-menu-item index="/analytics/dashboard">
              <el-icon><PieChart /></el-icon>
              <span>仪表盘</span>
            </el-menu-item>
            <el-menu-item index="/analytics/reports">
              <el-icon><TrendCharts /></el-icon>
              <span>报表</span>
            </el-menu-item>
          </el-sub-menu>
          
          <el-sub-menu index="/tools">
            <template #title>
              <el-icon><Tools /></el-icon>
              <span>系统工具</span>
            </template>
            <el-menu-item index="/tools/generator">
              <el-icon><Cpu /></el-icon>
              <span>代码生成</span>
            </el-menu-item>
            <el-menu-item index="/tools/swagger">
              <el-icon><Document /></el-icon>
              <span>系统接口</span>
            </el-menu-item>
          </el-sub-menu>
        </el-menu>
      </el-aside>
      
      <!-- 主要内容区域 -->
      <el-container>
        <!-- 顶部导航 -->
        <el-header>
          <div class="header-left">
            <div class="collapse-btn">
              <el-icon><Fold /></el-icon>
            </div>
            
            <!-- 顶部导航标签 -->
            <div class="top-tags">
              <el-tag
                class="top-tag"
                :effect="$route.path === '/users' ? 'dark' : 'plain'"
                @click="$router.push('/users')"
              >
                <el-icon><User /></el-icon> 用户管理
              </el-tag>
              
              <el-tag
                class="top-tag"
                :effect="$route.path === '/analytics' ? 'dark' : 'plain'"
                @click="$router.push('/analytics')"
              >
                <el-icon><DataAnalysis /></el-icon> 分析页
              </el-tag>
            </div>
          </div>
          
          <div class="header-right">
            <div class="search-container">
              <el-input
                placeholder="搜索用户"
                prefix-icon="Search"
                size="small"
                class="header-search"
              ></el-input>
            </div>
            
            <div class="header-actions">
              <el-tooltip content="切换主题" placement="bottom">
                <el-icon class="action-icon"><Moon /></el-icon>
              </el-tooltip>
              
              <el-tooltip content="全屏" placement="bottom">
                <el-icon class="action-icon"><FullScreen /></el-icon>
              </el-tooltip>
              
              <el-tooltip content="消息通知" placement="bottom">
                <el-badge :value="3" class="action-badge">
                  <el-icon class="action-icon"><Bell /></el-icon>
                </el-badge>
              </el-tooltip>
            </div>
            
            <el-dropdown @command="handleCommand">
              <div class="user-info">
                <el-avatar :size="32" class="user-avatar">A</el-avatar>
                <span class="user-name">{{ userStore.userInfo?.nickname || userStore.userInfo?.username || 'Admin' }}</span>
                <el-icon><ArrowDown /></el-icon>
              </div>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="profile">个人中心</el-dropdown-item>
                  <el-dropdown-item command="settings">系统设置</el-dropdown-item>
                  <el-dropdown-item command="logout" divided>退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </el-header>
        
        <!-- 内容区域 -->
        <el-main>
          <router-view :key="$route.fullPath" />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { computed, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessageBox } from 'element-plus'
import { 
  HomeFilled, 
  User, 
  Setting, 
  Lock, 
  Menu, 
  DataAnalysis, 
  PieChart, 
  TrendCharts, 
  Tools, 
  Cpu, 
  Document, 
  OfficeBuilding,
  Fold,
  Search,
  Moon, 
  FullScreen, 
  Bell, 
  ArrowDown 
} from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

// 当前激活的菜单
const activeMenu = computed(() => route.path)

// 处理下拉菜单命令
const handleCommand = (command: string) => {
  if (command === 'profile') {
    router.push('/profile')
  } else if (command === 'settings') {
    router.push('/settings')
  } else if (command === 'logout') {
    ElMessageBox.confirm('确定要退出登录吗?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(async () => {
      await userStore.logoutAction()
    }).catch(() => {})
  }
}

// 页面加载时获取用户信息
onMounted(async () => {
  if (userStore.token && !userStore.userInfo) {
    try {
      await userStore.fetchUserInfo()
    } catch (error) {
      console.error('获取用户信息失败', error)
    }
  }
})
</script>

<style scoped>
.layout-container {
  height: 100vh;
}

.el-container {
  height: 100%;
}

.el-aside {
  background-color: #001428;
  color: white;
  height: 100%;
  overflow-x: hidden;
}

.logo {
  height: 60px;
  display: flex;
  align-items: center;
  padding-left: 20px;
  color: #fff;
  font-size: 16px;
  background-color: #001428;
}

.logo-img {
  width: 32px;
  height: 32px;
  margin-right: 8px;
}

.el-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #fff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  padding: 0 20px;
  height: 60px;
}

.header-left {
  display: flex;
  align-items: center;
}

.collapse-btn {
  margin-right: 20px;
  font-size: 20px;
  cursor: pointer;
  color: #606266;
}

.top-tags {
  display: flex;
  align-items: center;
}

.top-tag {
  margin-right: 10px;
  cursor: pointer;
  padding: 5px 10px;
  display: flex;
  align-items: center;
}

.top-tag .el-icon {
  margin-right: 5px;
}

.header-right {
  display: flex;
  align-items: center;
}

.search-container {
  margin-right: 20px;
}

.header-search {
  width: 200px;
}

.header-actions {
  display: flex;
  align-items: center;
  margin-right: 20px;
}

.action-icon {
  font-size: 20px;
  margin: 0 10px;
  cursor: pointer;
  color: #606266;
}

.action-badge {
  margin: 0 10px;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.user-avatar {
  margin-right: 8px;
  background-color: #409EFF;
}

.user-name {
  margin-right: 5px;
  color: #333;
}

.el-main {
  background-color: #f0f2f5;
  padding: 20px;
  height: calc(100vh - 60px);
  overflow-y: auto;
}
</style> 